/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.xjnt.frameworks.web.jcaptcha;

import javax.servlet.http.HttpServletRequest;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;

/**
 * @FileName : JCaptcha.java
 * @Description : 
 * @Copyright : Copyright (c) 2015
 * @author caoyong
 * @date Jul 31, 2015 6:06:57 PM
 */
public class JCaptcha {
    public static final ManageableImageCaptchaService captchaService
            = new ManageableImageCaptchaService(new FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000, 75000);


    public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse) {
    	if(request.getSession(false) == null) return false;
    	
        boolean validated = false;
        try {
        	String id = request.getSession().getId();
            validated = captchaService.validateResponseForID(id, userCaptchaResponse).booleanValue();
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
        return validated;
    }

    public static boolean hasCaptcha(HttpServletRequest request, String userCaptchaResponse) {
    	if(request.getSession(false) == null) return false;
    	
        boolean validated = false;
        try {
        	String id = request.getSession().getId();
            System.out.println("校验ＩＤ:" + id);
            validated = captchaService.hasCapcha(id, userCaptchaResponse);
        } catch (CaptchaServiceException e) {
            e.printStackTrace();
        }
        return validated;
    }

}
